
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or (at
#  your option) any later version.
#
#  This program is distributed in the hope that it will be useful, but
#  WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

import sys
from qt import *
import pickle 
from superbatchmodeform import *

#
# in superbatchmode.py there ther is the straightforward implementation of this feature
# 



class superbatchmode(superbatchmodeform):
	def __init__(self,parent):
        	superbatchmodeform.__init__(self,parent)
		self.connect(self.addButton,SIGNAL('clicked()'),self.addClicked)
		self.connect(self.removeButton,SIGNAL('clicked()'),self.removeClicked)
		self.connect(self.removeButton,SIGNAL('clicked()'),self.removeClicked)
		self.connect(self.startButton,SIGNAL('clicked()'),self.startClicked)
		self.connect(self.stopButton,SIGNAL('clicked()'),self.stopClicked)
		self.connect(self.loadButton,SIGNAL('clicked()'),self.loadClicked)
		self.connect(self.saveButton,SIGNAL('clicked()'),self.saveClicked)
		
		QToolTip.add( self.addButton, "Add a (previously) saved pyvLab-file to the list")
		QToolTip.add( self.removeButton, "Remove the selected pyvLab-file from the list")
		QToolTip.add( self.loadButton, "Load a (previously) saved file list")
		QToolTip.add( self.saveButton, "Save this file list")
		QToolTip.add( self.clearButton, "Clears the file list")
		QToolTip.add( self.startButton, "Load the first file (in the list) and start measurement, then the next, and so on... ")
		QToolTip.add( self.stopButton, "stop all measurings")
		QToolTip.add( self.filelistBox, "Thes files will be loaded and measuremnts started (after \"Start SuperBatchModeMeasuring\"-Button was clicked)")
		QToolTip.add( self.loggingBox, "Loggings of the SuperBatchModeMeasuring")
		#QToolTip.add( self.dateiendung, "Add this file extension to the saved file")
		self.filePos=0
		self.running=False
	
	
	def saveClicked(self):
		print "save"
		s=""
		s = QFileDialog.getSaveFileName("","pyvLab SuperBatchMode File (*.sbm)",self,"File To Open","File To Open" )
		s=str(s)
		if len(s)>0:
			f = open(s, 'w')
			i=0
			sav=[]
			while i<self.filelistBox.count():
				sav.append(str(self.filelistBox.text(i)))
				i+=1
			pickle.dump(sav, f)
			f.close()
		
	def loadClicked(self):
		print "load"
		s=""
		s = QFileDialog.getOpenFileName("","pyvLab SuperBatchMode File (*.sbm)",self,"File To Open","File To Open" )
		s=str(s)
		if len(s)>0:
			f = open(s, 'r')
			sav=pickle.load(f)
			self.filelistBox.clear()
			for x in sav:
				self.filelistBox.insertItem(x)
			f.close()
	
	def addClicked(self):
		s = QFileDialog.getOpenFileName("","pyvLab File (*.pyv)",self,"File To Open","File To Open" )
		s=str(s)
		self.filelistBox.insertItem(s)
		
	def removeClicked(self):
		i=self.filelistBox.currentItem()
		self.filelistBox.removeItem(i)
		
	def startClicked(self):
		print "superBatchMode start"
		self.filePos=0
		if self.filePos<self.filelistBox.count():
			file=str(self.filelistBox.text(self.filePos))
			self.loggingBox.clear()
			self.loggingBox.insertItem("Starting SuperBatchMode...")
			self.loggingBox.insertItem("Loading "+file)
			self.loggingBox.setCurrentItem(self.loggingBox.count()-1)
			self.loggingBox.ensureCurrentVisible()			
			self.emit(PYSIGNAL('autoMeasureFile'),(file,))
			self.startButton.setEnabled(False)
			self.stopButton.setEnabled(True)
			self.running=True
			
	def stopClicked(self):
		print "superBatchMode stop"
		self.startButton.setEnabled(True)
		self.stopButton.setEnabled(False)
		self.running=False
		self.emit(PYSIGNAL('stopMeasure'),())
		self.loggingBox.insertItem("SuperBatchMeasuring stopped")
		self.loggingBox.setCurrentItem(self.loggingBox.count()-1)
		self.loggingBox.ensureCurrentVisible()
		
	
	def nextFile(self):
		if self.running==False:
			return False
		print "superBatchMode nextFile"
		self.filePos+=1
		if self.filePos<self.filelistBox.count():
			file=str(self.filelistBox.text(self.filePos))
			self.loggingBox.insertItem("Loading "+file)
			self.loggingBox.setCurrentItem(self.loggingBox.count()-1)
			self.loggingBox.ensureCurrentVisible()			
			self.emit(PYSIGNAL('autoMeasureFile'),(file,))
		else:
			self.startButton.setEnabled(True)
			self.stopButton.setEnabled(False)
			self.loggingBox.insertItem("Finished SuperBatchMeasuring")
			self.loggingBox.setCurrentItem(self.loggingBox.count()-1)
			self.loggingBox.ensureCurrentVisible()
			self.running=False
	
	
	